package com.example.exam.service.impl;

import com.example.exam.pojo.Exception.ServiceException;
import com.example.exam.mapper.TSubjectMapper;
import com.example.exam.pojo.vo.SubjectVO.SubjectStandardVO;
import com.example.exam.service.ISubjectService;
import com.example.exam.tools.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class SubjectServiceImpl implements ISubjectService {

    @Autowired
    private TSubjectMapper subjectMapper;

    public SubjectServiceImpl() {
        log.info("创建业务对象：SubjectServiceImpl");
    }

    @Override
    public List<SubjectStandardVO> listByPage(SubjectStandardVO subjectStandardVO) {
        log.debug("开始处理【分页查询学科列表】的业务");
        return subjectMapper.listByPage(subjectStandardVO);
    }

    @Override
    public List<SubjectStandardVO> list() {
        log.debug("开始处理【查询所有学科列表】的业务");
        return subjectMapper.list();
    }

    @Override
    public void addNew(SubjectStandardVO subjectStandardVO) {
        log.debug("开始处理【添加学科】的业务，参数：{}", subjectStandardVO);
        String name = subjectStandardVO.getName();
        String levelName = subjectStandardVO.getLevelName();

        int count = subjectMapper.count(name, levelName);
        if (count > 0) {
            String message = "该学科信息已存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        subjectStandardVO.setDeleted(false);
        log.debug("即将向数据库中插入数据：{}", subjectStandardVO);
        int row = subjectMapper.insert(subjectStandardVO);
        if (row != 1) {
            String message = "添加学科失败！服务器忙，请稍后再次尝试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public void update(SubjectStandardVO subjectStandardVO) {
        log.debug("开始处理【修改学科】的业务，参数：{}", subjectStandardVO);
        int count = subjectMapper.count(subjectStandardVO.getName(), subjectStandardVO.getLevelName());
        if (count > 0) {
            String message = "该学科信息已存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        int row = subjectMapper.update(subjectStandardVO);
        if (row != 1) {
            String message = "修改学科失败！服务器忙，请稍后重试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public void deleteById(Integer id) {
        log.info("开始处理【删除学科】的业务，参数：{}", id);
        SubjectStandardVO subjectStandardVO = subjectMapper.getStandardById(id);
        subjectStandardVO.setDeleted(true);
        int count = subjectMapper.update(subjectStandardVO);
        if (count != 1) {
            String message = "删除学科失败！服务器忙，请稍后重试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public SubjectStandardVO getStandardById(Integer id) {
        log.debug("开始处理【根据id查询学科】的业务");
        SubjectStandardVO subject = subjectMapper.getStandardById(id);
        return subject;
    }


}
